<?php

namespace App\Http\Controllers\Admin;
use App\Models\Notice;


/**
 * 商品分类管理控制器
 */
class NoticeController extends BaseController
{

    public function __construct(Notice $notice)
    {
        parent::__construct();
        $this->notice = $notice;
    }

    /**
     * 分类管理列表
     */
    public function getIndex()
    {
        $tree = $this->brand->getTree(0, 'id,title,sort,pid,status');
        return view('admin.brand.index', compact('tree'));
    }

    /* 新增分类 */
    public function getAdd()
    {
        $brand = $this->brand->checkParentCategory();
        if (!$brand) {
            return $this->error($this->brand->getError() ? $this->brand->getError() : '未知错误');
        }
        $info['pid'] = request('pid');
        return view('admin.brand.edit', compact('category', 'info'));
    }

    public function postAdd()
    {
        $res = $this->brand->addOrUpdate();
        if (!$res) {
            return $this->error($this->brand->getError() ? $this->brand->getError() : '新增失败');
        }
        return $this->success('新增成功', urlAdmin('brand', 'getIndex', ['pid' => request('pid')]));
    }

    /* 编辑分类 */
    public function getEdit()
    {
        $brand = $this->brand->checkParentCategory();
        if (!$brand) {
            return $this->error($this->brand->getError() ? $this->brand->getError() : '未知错误');
        }
        /* 获取分类信息 */
        $info = request('id') ? $this->brand->getInfo(request('id')) : '';
        return view('admin.brand.edit', compact('info', 'category'));
    }

    public function postEdit()
    {
        $res = $this->brand->addOrUpdate();
        if (!$res) {
            return $this->error($this->brand->getError() ? $this->brand->getError() : '未知错误');
        }
        return $this->success('更新成功', urlAdmin('brand', 'getIndex', ['pid' => request('pid')]));
    }


    /**
     * 删除一个分类
     */
    public function getRemove()
    {
        $res = $this->brand->del();
        if (!$res) {
            return $this->error($this->brand->getError());
        }
        return $this->success($res);
    }

    /**
     * 操作分类初始化
     */
    public function getOperate($type)
    {
        //检查操作参数
        $operate = $type == 'move' ? '移动' : ($type == 'merge' ? '合并' : '');
        if (!$operate) {
            return $this->error('参数错误！');
        }
        $from = intval(request('from'));
        if (empty($from)) {
            return $this->error('参数错误！');
        }
        //获取分类
        $where = ['status' => 1, 'id' => ['<>', $from]];
        $list = $this->brand->_getList($where, 'id,pid,title')->toArray();
        //移动分类时增加移至根分类
        if (strcmp($type, 'move') == 0) {
            //不允许移动至其子孙分类
            $list = tree_to_list(list_to_tree($list));
            $brand = $this->brand->_getOneById($from, 'pid');
            $brand['pid'] && array_unshift($list, ['id' => 0, 'title' => '根分类']);
        }
        return view('admin.brand.operate', compact('type', 'operate', 'from', 'list'));
    }

    public function postOperate()
    {
        $res = $this->brand->operate(request('type'));
        if (!$res) {
            return $this->error($this->brand->getError());
        }
        return $this->success($res, urlAdmin('brand', 'getIndex'));
    }

}
